using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Devices.Sensors;

namespace Bounce
{
    public class TiltingBalls : GameType
    {
        public TiltingBalls(GamePage oPage)
            : base(oPage)
        { }

        #region Screen Methods

        public override void Initialize(ContentManager oContentManager, GraphicsDevice oDevice)
        {
            base.Initialize(oContentManager, oDevice);

            Resolution = new Vector2(oDevice.Viewport.Width, oDevice.Viewport.Height);

            Texture2D texture_Basketball = oContentManager.Load<Texture2D>("Circle");
            Texture2D texture_Rim = oContentManager.Load<Texture2D>("Circle");

            BounceFriction = 0.0f;
            RollFriction = 0.0f;
            AirFriction = 0.05f;

            for (int i = 0; i < 15; i++)
            {
                int row = i % 3;
                int column = i % 5;
                Vector2 vPosition = new Vector2(40 + row * 100, 300 + (column * 100));

                BasketBalls.Add(new Basketball(texture_Basketball, vPosition, 30.0f, 2.0f));
            }

            Goals.Add(new Goal(new Vector2(200, 200), texture_Rim));

            accelReading = Vector3.Zero;
            accelSensor = new Accelerometer();
            accelSensor.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(accelSensor_CurrentValueChanged);

            // Start the accelerometer
            try
            {
                accelSensor.Start();
                accelActive = true;
            }
            catch (AccelerometerFailedException e)
            {
                // the accelerometer couldn't be started.  No fun!
                accelActive = false;

            }
            catch (UnauthorizedAccessException e)
            {
                // This exception is thrown in the emulator-which doesn't support an accelerometer.
                accelActive = false;
            }
        }

        void accelSensor_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
        {
            accelReading = e.SensorReading.Acceleration;
        }

        #endregion

        protected override void CheckCollisions()
        {
            CheckCollisions_Balls();
            CheckCollisions_Goals();
            CheckCollisions_Walls();
        }

        protected override void DrawGameType()
        {
        }

        protected override void Input()
        {
            Vector2 gravity = new Vector2(accelReading.X, accelReading.Y);
            gravity.Y *= -1;
            if (gravity.Length() > 0.1f)
                gravity.Normalize();

            GravityDirection = gravity;
            GravityStrength = 0.52f * gravity.Length();
        }


        Accelerometer accelSensor;
        Vector3 accelReading;
        bool accelActive;

    }
}
